<?php

$first_day = mktime(0,0,0,$month, 1, $year);
$num_days = cal_days_in_month(0, $month, $year);
// $day_counter: begins from 1, extends to last day of month ($num_days)
// $day_header: counter to keep track of which day of the week; 1->Sun, 7-> Sat
//$offset: counter to determine the number of blank days in the beginning of each month
$day_counter = 1;
$day_header = 1;

$day_of_week = date('D', $first_day);
switch ($day_of_week) {
	case "Sun": $offset = 0; break;
	case "Mon"; $offset = 1; break;
	case "Tue"; $offset = 2; break;
	case "Wed"; $offset = 3; break;
	case "Thu"; $offset = 4; break;
	case "Fri"; $offset = 5; break;
	case "Sat"; $offset = 6; break;
}

	$curday=date("j");
	$curmonth=date("n");
	$curyear=date("z");
	$json = new Zend_Json();
?>

<CENTER>
<TABLE class="monthdisplay" border=1 cellspacing=0>
<TR> <TH  class="monthdisplay" height=10>Sun</TH> <TH  class="monthdisplay">Mon</TH> <TH  class="monthdisplay">Tues</TH> <TH  class="monthdisplay">Wed</TH> <TH  class="monthdisplay">Thurs</TH> <TH  class="monthdisplay">Fri</TH> <TH  class="monthdisplay">Sat</TH>    </TR>
<TR>
	<?php

 //make a 2D array of 35*blank
/*	$days=null;

	$days=array();*/
  for ($i=0;$i<42;$i++){
    $days[$i]=array();
  }

        //find events and put them in there
        //$events=getAllEvents($client);
				$events=outputCalendarByDateRange($client, "$year-$month-01", "$year-$month-$num_days");
        $tail_days=(35-cal_days_in_month(0,$month,$year)-offset);
	
	foreach ($events as $event) {
                $parseMeArray=$event->when;
                foreach($parseMeArray as $parseMe){
                	//pull out month day year
                	$eYear=substr($parseMe,8,4);
                	$eMonth=substr($parseMe,13,2);
                	$eDay=substr($parseMe,16,2);
                	//remove leading 0's if any
                	if (substr($eMonth,0,1)=="0"){
                 	       $eMonth=substr($eMonth,1,1);
                	}
//			echo "month better be 3 still ".$month;
        		if ($eMonth==($month-1)){
        	                if ($offset>0)
        	                {
        	                        $days[$offset - ($prev_month_offset - $eDay)-1][]=$event; 
        	                }
        	        } else if ($eMonth==$month){
        	                $days[$eDay+$offset-1][]=$event;
	
       	         } else if ($eMonth==($month+1) && $eDay<$tail_days){
		   							$days[$numdays+$offset+$eDay-1][]=$event;
                	}
								}
  }

        $index=0;

	while ($offset > 0)
	{	
		echo "<TD align=left valign=top ><font color=silver size=1>";
		echo ($prev_month_offset - $offset+1).'<br>';
		$offset--;
		$day_counter++;
                echo "</font>";
                echo "<DIV id='onTop'>";
			foreach($days[$index] as $event){
				$ev = $json->encode($event);
				$eventTime = substr(($event->when[0]),19,5);
				if ($eventTime == 'Ends:') {
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$event->title."</a></li>";
				}
				else { // it is an all day event
					$eventTime = date("g:i A", strtotime($eventTime));
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$eventTime.": ".$event->title."</a></li>";
				}
			}
                echo "</DIV>";
		
                echo "</TD>";
                $index++;



	}
	while ($day_header <= $num_days) 
	{

		if ($day_header == $system_day && $month == $system_month && $year == $system_year)
		{
			echo "<TD align=left valign=top  bgcolor='#FDECB2'>";
                        echo "<a href='viewDay.php?day=$day_header&month=$month&year=$year' class='day_header'>$day_header</a><br>";
                        echo "</font>";
                        echo "<DIV id='onTop'>";
			foreach($days[$index] as $event){
				$ev = $json->encode($event);
				$eventTime = substr(($event->when[0]),19,5);
				if ($eventTime == 'Ends:') {
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$event->title."</a></li>";
				}
				else { // it is an all day event
					$eventTime = date("g:i A", strtotime($eventTime));
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$eventTime.": ".$event->title."</a></li>";
				}
			}
			echo "</DIV>";
		}
		else
		{
	                echo "<TD align=left valign=top >";
			echo "<a href='viewDay.php?day=$day_header&month=$month&year=$year' class='day_header'>$day_header</a><br>";
                        echo "<DIV id='onTop'>";

                        foreach($days[$index] as $event){
				$ev = $json->encode($event);
				$eventTime = substr(($event->when[0]),19,5);
				if ($eventTime == 'Ends:') {
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$event->title."</a></li>";
				}
				else { // it is an all day event
					$eventTime = date("g:i A", strtotime($eventTime));
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$eventTime.": ".$event->title."</a></li>";
				}
			}
                        echo "</DIV>";
		}
		echo "</TD>";
		$index++;
		$day_header++;
		$day_counter++;
		if ($day_counter > 7 && $day_header <= $num_days)
			{
		 	echo "</TR><TR>";
			$day_counter = 1;
			}
	}
	$next_offset = 1;
	while ($day_counter <= 7 && $day_counter != 1) 
	{
 	        echo "<TD align=left valign=top ><font color=silver size=1>";
                echo $next_offset."<br>";
                echo "</font>";
                        echo "<DIV id='onTop'>";

			foreach($days[$index] as $event){
				$ev = $json->encode($event);
				$eventTime = substr(($event->when[0]),19,5);
				if ($eventTime == 'Ends:') {
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$event->title."</a></li>";
				}
				else { // it is an all day event
					$eventTime = date("g:i A", strtotime($eventTime));
					echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin-bottom:2px; cursor:default'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$eventTime.": ".$event->title."</a></li>";
				}
			}
                echo "</DIV>";

                echo "</TD>";
                $day_counter++;
                $next_offset++;
                $index++;

	}
	$days=null;
	?>
</TR>

</TABLE>
</CENTER>
